package com.wh.minihelp.flower.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.wh.db.jdbc.mysql.IJDBCRowMapper;
import com.wh.db.jdbc.mysql.IJDBCRowSetter;
import com.wh.db.sql.SQLUtils;
import com.wh.minihelp.flower.domain.ChildFlower;
import com.wh.minihelp.flower.domain.FamilyFlower;
import com.wh.minihelp.user.dao.UserDBMetas;

public class FlowerDBMetas {
	public static final String TN_FamilyFlower = "wh_flower_family_flower"//
			, TN_ChildFLower = "wh_flower_child_flower";// 小孩红花记录;

	// 获取家庭的红花类别
	public static class FamilyFlowerMapper implements IJDBCRowMapper<FamilyFlower> {
		static String sql_all = "select id,t_familyId,t_type,t_flowerName,t_createDate,t_order"//
				+ " from " + TN_FamilyFlower //
		;
		static String sql_order = " order by t_type,t_order,t_createDate";// 按类型和创建日期排序

		public FamilyFlower mapRow(ResultSet rs, int rowNum) throws SQLException {
			FamilyFlower ff = new FamilyFlower();
			ff.setId(rs.getString(1));
			ff.setFamilyId(rs.getString(2));
			ff.setType(rs.getInt(3));
			ff.setName(rs.getString(4));
			ff.setCreateDate(rs.getDate(5));
			ff.setOrder(rs.getInt(6));
			return ff;
		}
	}

	// 保存家庭的红花类别
	public static class FamilyFlowerSetter implements IJDBCRowSetter<FamilyFlower> {
		static String SQL_INSERT = "insert into " + TN_FamilyFlower //
				+ "(id,t_familyId,t_type,t_flowerName,t_createDate,t_order)values(?,?,?,?,?,?)";//
		// static String SQL_UPDATE_ORDER = "update " + TN_FamilyFlower + "set t_order =? where id=?";//

		public void setValues(PreparedStatement ps, FamilyFlower o) throws SQLException {
			ps.setString(1, o.getId());
			ps.setString(2, o.getFamilyId());
			ps.setInt(3, o.getType());
			ps.setString(4, o.getName());
			ps.setString(5, SQLUtils.toSQLDate(o.getCreateDate()));
			ps.setInt(6, o.getOrder());
		}
	}

	// 修改红花顺序setter
	public static class FamilyFlowerUpdateSetter implements IJDBCRowSetter<FamilyFlower> {
		static String SQL_UPDATE_ORDER = "update " + TN_FamilyFlower + " set t_order =? where id=?";//

		public void setValues(PreparedStatement ps, FamilyFlower o) throws SQLException {
			ps.setString(2, o.getId());
			ps.setInt(1, o.getOrder());
		}
	}

	// 获取家庭的红花类别
	public static class ChildFlowerMapper implements IJDBCRowMapper<ChildFlower> {
		static String sql_all = "select id,t_childId,t_flowerDate,t_flowerDesc,t_flowerNumber,t_flowerName,t_operAcount"//
				+ " from " + TN_ChildFLower //
		;
		static String sql_order = " order by t_flowerDate desc";// 创建日期排序

		public ChildFlower mapRow(ResultSet rs, int rowNum) throws SQLException {
			ChildFlower ff = new ChildFlower();
			ff.setId(rs.getString(1));
			ff.setChildId(rs.getString(2));
			ff.setFlowerDate(rs.getDate(3));
			ff.setFlowerDesc(rs.getString(4));
			ff.setFlowerNumber(rs.getInt(5));
			ff.setFlowerName(rs.getString(6));
			ff.setOperAcount(rs.getString(7));
			return ff;
		}
	}

	// 获取最新的红花信息
	public static class NewChildFlowerMapper implements IJDBCRowMapper<ChildFlower> {
		static String sql_all = "select cf.id,cf.t_childId,cf.t_flowerDate,cf.t_flowerDesc,cf.t_flowerNumber,cf.t_flowerName,cf.t_operAcount,ci.t_name "//
				+ " from " + TN_ChildFLower + " cf," + UserDBMetas.TN_ChildInfo + " ci"//
				+ " where cf.t_childId =ci.id "//
		;
		static String sql_order = " order by t_flowerDate desc";// 创建日期排序

		public ChildFlower mapRow(ResultSet rs, int rowNum) throws SQLException {
			ChildFlower ff = new ChildFlower();
			ff.setId(rs.getString(1));
			ff.setChildId(rs.getString(2));
			ff.setFlowerDate(rs.getDate(3));
			ff.setFlowerDesc(rs.getString(4));
			ff.setFlowerNumber(rs.getInt(5));
			ff.setFlowerName(rs.getString(6));
			ff.setOperAcount(rs.getString(7));
			ff.setChildName(rs.getString(8));
			return ff;
		}
	}

	// 保存家庭的红花类别
	public static class ChildFlowerSetter implements IJDBCRowSetter<ChildFlower> {
		static String SQL_INSERT = "insert into " + TN_ChildFLower //
				+ "(id,t_childId,t_flowerDate,t_flowerDesc,t_flowerNumber,t_flowerName,t_operAcount)values(?,?,?,?,?,?,?)";//

		public void setValues(PreparedStatement ps, ChildFlower o) throws SQLException {
			ps.setString(1, o.getId());
			ps.setString(2, o.getChildId());
			ps.setString(3, SQLUtils.toSQLDate(o.getFlowerDate()));
			ps.setString(4, o.getFlowerDesc());
			ps.setInt(5, o.getFlowerNumber());
			ps.setString(6, o.getFlowerName());
			ps.setString(7, o.getOperAcount());
		}
	}

}